package com.group7.mall.controller;

import com.group7.mall.bean.Result;
import com.group7.mall.convert.GoodsConverter;
import com.group7.mall.dto.GoodsAddDto;
import com.group7.mall.dto.GoodsSpecificationsDto;
import com.group7.mall.dto.SellerGoodsDetailDto;
import com.group7.mall.dto.SellerGoodsPreviewDto;
import com.group7.mall.entity.TbGoods;
import com.group7.mall.entity.TbGoodsInventoryPrice;
import com.group7.mall.req.BaiduImageRequest;
import com.group7.mall.req.SellerGoodsPageRequest;
import com.group7.mall.resp.SellerGoodsPageResponse;
import com.group7.mall.service.SellerGoodsService;
import com.group7.mall.service.UserGoodsService;
import lombok.Data;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;


@RequestMapping("seller-goods")
@RestController
public class SellerGoodsController {

    @Resource
    private SellerGoodsService sellerGoodsService;

    @PostMapping("show-goods-detail")
    public Result<SellerGoodsDetailDto> showGoodsDetail(@RequestBody SellerGoodsDetailDto sellerGoodsDetailDto) {
        TbGoods tbGoods = sellerGoodsService.queryById(sellerGoodsDetailDto.getGoodsId());
        sellerGoodsDetailDto = GoodsConverter.INSTANCE.convertSellerGoodsDetailDtoToTbGoods(tbGoods);
        sellerGoodsDetailDto = sellerGoodsService.getGoodsSpecificationsDto(sellerGoodsDetailDto);
        sellerGoodsDetailDto = sellerGoodsService.getGoodsParameter(sellerGoodsDetailDto);
        sellerGoodsDetailDto = sellerGoodsService.getGoodsImages(sellerGoodsDetailDto);
        return Result.ok(sellerGoodsDetailDto);
    }

    @PostMapping("soft-delete-goods")
    public Result<Integer> softDeleteGoods(@RequestBody TbGoods tbGoods) {
        return Result.ok(sellerGoodsService.softDeleteGoods(tbGoods));
    }

    @PostMapping("ban-goods")
    public Result<Integer> banGoods(@RequestBody TbGoods tbGoods) throws IOException {
        return Result.ok(sellerGoodsService.banGoods(tbGoods));
    }

    @PostMapping("unban-goods")
    public Result<Integer> unbanGoods(@RequestBody TbGoods tbGoods) throws IOException {
        return Result.ok(sellerGoodsService.unbanGoods(tbGoods));
    }

    @PostMapping("show-goods")
    public Result<SellerGoodsPageResponse<SellerGoodsPreviewDto>> sellerShowGoodsByPage(@RequestBody SellerGoodsPageRequest sellerGoodsPageRequest) {
        return Result.ok(sellerGoodsService.sellerShowGoodsByPage(sellerGoodsPageRequest));
    }

    @PostMapping("update-goods-parameter")
    @Transactional
    public Result<GoodsAddDto> updateGoodsParameter(@RequestBody GoodsAddDto goodsAddDto) {
        Integer goodsId = goodsAddDto.getGoodsId();
        sellerGoodsService.deleteAllGoodsParameter(goodsId);
        goodsAddDto = sellerGoodsService.addGoodsParameter(goodsAddDto);
        return Result.ok(goodsAddDto);
    }

    @PostMapping("add-goods")
    @Transactional
    public Result<GoodsAddDto> addGoods(@RequestBody GoodsAddDto goodsAddDto) throws IOException {
        TbGoods tbGoods = sellerGoodsService.sellerAddGoods(goodsAddDto);
        goodsAddDto.setGoodsId(tbGoods.getGoodsId());
        goodsAddDto = sellerGoodsService.addGoodsAttribute(goodsAddDto);
        goodsAddDto = sellerGoodsService.addGoodsType(goodsAddDto);
        goodsAddDto = sellerGoodsService.addGoodsInventoryPrice(goodsAddDto);
        goodsAddDto = sellerGoodsService.addGoodsParameterAndImages(goodsAddDto);
        return Result.ok(goodsAddDto);
    }

    @PostMapping("show-unban-goods-type")
    @Transactional
    public Result<List<GoodsSpecificationsDto>> showUnbanGoodsType(@RequestBody SellerGoodsDetailDto sellerGoodsDetailDto) {
        sellerGoodsDetailDto = sellerGoodsService.getGoodsSpecificationsDto(sellerGoodsDetailDto);
        return Result.ok(sellerGoodsDetailDto.getGoodsSpecificationsDtoList());
    }

    @PostMapping("update-unban-goods-type")
    @Transactional
    public Result<Integer> updateUnbanGoodsType(@RequestBody List<GoodsSpecificationsDto> goodsSpecificationsDtoList) {
        goodsSpecificationsDtoList.forEach(goodsSpecificationsDto -> {
            System.out.println(goodsSpecificationsDto.toString());
        });
        return Result.ok(sellerGoodsService.updateUnbanGoodsType(goodsSpecificationsDtoList));
    }

    @PostMapping("update-goods-info")
    @Transactional
    public Result<GoodsAddDto> updateGoodsImage(@RequestBody GoodsAddDto goodsAddDto) throws IOException {
        sellerGoodsService.updateAllGoodsInformation(goodsAddDto);
        return Result.ok(goodsAddDto);
    }

    @PostMapping("update-ban-goods-info")
    @Transactional
    public Result<Integer> updateBanGoodsInfo(@RequestBody GoodsAddDto goodsAddDto) throws IOException {
        Integer goodsId = goodsAddDto.getGoodsId();
        System.out.println(goodsId + " 666666");
        // TODO 参数、介绍、图片等更新
        sellerGoodsService.updateAllGoodsInformation(goodsAddDto);
        sellerGoodsService.deleteAllGoodsType(goodsId);
        goodsAddDto.setGoodsId(goodsId);
        goodsAddDto = sellerGoodsService.addGoodsAttribute(goodsAddDto);
        goodsAddDto = sellerGoodsService.addGoodsType(goodsAddDto);
        goodsAddDto = sellerGoodsService.addGoodsInventoryPrice(goodsAddDto);
        int i = sellerGoodsService.updateAllGoodsInformation(goodsAddDto);
        return Result.ok(i);
    }
}



